package com.lib.controller.admin;

import com.lib.pojo.DTO.CategoryDTO;
import com.lib.pojo.DTO.PageDTO;
import com.lib.pojo.VO.CategoryVO;
import com.lib.pojo.entity.Category;
import com.lib.pojo.entity.PageResult;
import com.lib.pojo.entity.Result;
import com.lib.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*")
@RestController("CategoryControllerAdmin")
@Slf4j
@Tag(name = "分类管理端")
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //1:根据ID类型查询分类
    @Operation(summary = "根据分类ID查询图书")
    @GetMapping("/categoryId")
    public Result<List<CategoryVO>> getCategory(Integer categoryId){
        log.info("根据分类id查询图书:{}",categoryId);
        List<CategoryVO> categoryVOS = categoryService.getCategoryById(categoryId);
        return Result.success(categoryVOS);
    }
    //2:根据id删除分类
    @Operation(summary = "根据分类ID删除图书")
    @DeleteMapping
    public Result delete(Integer categoryId){
        log.info("根据分类id删除图书:{}",categoryId);
        categoryService.delete(categoryId);
        return Result.success();
    }
    //3:新增分类
    @Operation(summary = "新增分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }
    //4:启用、禁用分类
    @Operation(summary = "启用、禁用分类")
    @PostMapping("/status/{status}")
    public Result statusStartOrStop(@PathVariable Integer status,Integer categoryId){
        log.info("启用、禁用分类:{}",categoryId);
        categoryService.statusStartOrStop(status,categoryId);
        return Result.success();
    }
    //5：修改分类
    @Operation(summary = "修改分类")
    @PutMapping
    public Result putCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类:{}",categoryDTO);
        categoryService.putCategory(categoryDTO);
        return Result.success();
    }
    //6：分类分页查询
    @Operation(summary = "分类分页查询")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody PageDTO pageDTO){
        log.info("分类分页查询:{}",pageDTO);
        PageResult pageResult = categoryService.page(pageDTO);
        return Result.success(pageResult);
    }
    //7：根据分类ID查询一个分类
    @Operation(summary = "根据分类ID查询一个分类")
    @GetMapping("/id")
    public Result<Category> getById(Integer id){
        log.info("根据分类ID查询一个分类:{}",id);
        Category category = categoryService.getById(id);
        return Result.success(category);
    }
    //8：批量删除分类
    @Operation(summary = "批量删除分类")
    @DeleteMapping("/ids")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        log.info("批量删除分类:{}",ids);
        categoryService.deleteByIds(ids);
        return Result.success();
    }

}
